import requests
from lxml import etree
import json
def save_file(filename,content):
    with open(filename,"w+",encoding="utf-8") as f:
        f.write(json.dumps(content,ensure_ascii=False,indent=2))
def do_request(url):
    response = requests.get(url)
    response.encoding = "utf-8"
    return response.text
def parse_page(html):
    node = etree.HTML(html)
    titles = node.xpath("//th[@class='new forumtit']/a[@class='s xst']/text()")
    author = node.xpath("//th[@class='new forumtit']/div[@class='foruminfo']/i[@class='z']/a/span/text()")
    dates = node.xpath("//th[@class='new forumtit']/div[@class='foruminfo']/i[@class='z']/span[1]/text()")
    items = []
    for i in range(len(titles)):
        d = {"标题":titles[i],
             "作者":author[i],
             "时间":str(dates[i]).strip("@ ")}
        items.append(d)
    return items
def create_url(start_page,end_page):
    base_url = "https://bbs.itheima.com/forum-425-"
    for i in range(start_page,end_page+1):
        url = base_url+str(i)+".html"
        html = do_request(url)
        items = parse_page(html)
        save_file("论坛信息/"+str(i)+".json",items)
if __name__ == '__main__':
    create_url(1,5)